var ajaxBusy = false;

function initializeAjax() {
  var xmlreq = false;
  if (window.XMLHttpRequest) {
    // FireFox, Opera, Safari
    xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      // Internet Explorer
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      try {
        // stary Internet Explorer
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        alert("Twoja przegladarka nie wspiera AJAX");
		return false;
      }
    }
  }

  return xmlreq;
}

function xmlValidate(xml) {
	
	try {
	
	if (xmlObj.childNodes[0].tagName == "error" 
		&& xmlObj.childNodes[1].tagName == "method"
		&& xmlObj.childNodes[2].tagName == "result" ) {
			if (xmlObj.childNodes[0].textContent != "") {
				error_info(xmlObj.childNodes[0].textContent);
				return false;
			}
		}
	} catch (ex) {
		alert ('Wystąpił błąd podczas komunikacji z serwerem ' + ex);
	}
	return true;
}

function xmlGetResult(xml, methodName) {
	try {
		if(xmlObj.childNodes[1].textContent == methodName){
			return xmlObj.childNodes[2].textContent;
		}
	} catch (ex){
		error_info('xmlGetResult error');
	}
	return false;
}

function doAjax(postData, postDestination, handler) {
	if (ajaxBusy) {
		debug_info('doAjax','Jesteśmy w trakcie komunikacji z serwerem');
		return false;
	}
	// najpierw inicjujemy sobie ajaxa
	if(!initializeAjax()) error_info('doAjax', 'Błąd inicjalizacji!');
	ajaxBusy = true;
	xmlHttp.onreadystatechange = handler;
	xmlHttp.open("POST",postDestination,true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(postData);
	return true;
}

function createQuery(form)
{
    var elements = form.elements;
    var pairs = new Array();
    for (var i = 0; i < elements.length; i++) {

        if ((name = elements[i].name) && (value = elements[i].value))
            pairs.push(name + "=" + encodeURIComponent(value));
    }
    return pairs.join("&");
}

var loaderCheck = false;
function loaderOn() {
		debug_info('loaderOn','start');
	if (!loaderCheck) {
		opacity("loader", 0, 100, 200);
	}
	loaderCheck=true;
}

function loaderOff() {
		debug_info('loaderOff','start');
	if (loaderCheck) {
		opacity("loader", 100, 0, 200);
	}	
	loaderCheck=false;
}
